package com.atjg.autocat.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.atjg.autocat.R;


public class UpdateDialog extends Dialog implements View.OnClickListener {
    private TextView description;
    private OnUpdataListener onUpdataListener;
    private LinearLayout llUpdata;
    private RelativeLayout ll_progress;
    private ProgressBar progressBar;
    private TextView tv_num;
    private ImageView ivClose;
    private TextView tv_version;

    public void setOnUpdataListener(OnUpdataListener onUpdataListener) {
        this.onUpdataListener = onUpdataListener;
    }

    public UpdateDialog(@NonNull Context context) {
        super(context, R.style.UpdateDialog);
        init(context);
    }

    /**
     * 初始化布局
     */
    private void init(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_update, null);
        setContentView(view);
        setWindowSize(context);
        initView(view);
    }

    private void initView(View view) {
        description = view.findViewById(R.id.tv_description);
        llUpdata = view.findViewById(R.id.llUpdata);
        ll_progress = view.findViewById(R.id.llProgress);
        progressBar = view.findViewById(R.id.progressBar);
        tv_num = view.findViewById(R.id.tvNum);
        tv_version = view.findViewById(R.id.tv_version);
        ivClose = view.findViewById(R.id.ivClose);
        ivClose.setOnClickListener(this);
        llUpdata.setOnClickListener(this);
    }

    @SuppressLint("SetTextI18n")
    public void setVersion(String content) {
        //设置界面数据
        tv_version.setText( "新版本号：" + content);
    }

    public void setContent(String content) {
        if (description != null) {
            description.setText(content);
        }
    }

    public void showClose(boolean state) {
        if (ivClose != null) {
            if (state) {
                ivClose.setVisibility(View.VISIBLE);
            } else {
                ivClose.setVisibility(View.GONE);
            }
        }
    }

    private void setWindowSize(Context context) {
        Window dialogWindow = this.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.gravity = Gravity.CENTER;
        dialogWindow.setAttributes(lp);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.ivClose) {
            if (onUpdataListener != null) {
                dismiss();
                onUpdataListener.upClose();
            }
        } else if (id == R.id.llUpdata) {
            hintUpdata();
            if (onUpdataListener != null) {
                onUpdataListener.upData();
            }
        }
    }

    public void hintUpdata() {
        if (llUpdata != null && ll_progress != null) {
            llUpdata.setVisibility(View.GONE);
            ll_progress.setVisibility(View.VISIBLE);
        }
    }

    public void hintClose() {
        if (ivClose != null) {
            ivClose.setVisibility(View.GONE);
        }
    }

    public void showProgress(int progress) {
        if (tv_num != null && progressBar != null) {
            progressBar.setProgress(progress);
            tv_num.setText(progress + "%");
            if (progress >= 50) {
                tv_num.setTextColor(Color.parseColor("#FFFFFF"));
            }
        }
    }

    public interface OnUpdataListener {
        void upData();

        void upClose();
    }
}
